package com.lzb.musicplayer.adapter;

import android.graphics.Bitmap;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.lzb.basic.ProviderMultiAdapter;
import com.lzb.basic.utils.TimeUtil;
import com.lzb.musicplayer.R;
import com.lzb.musicplayer.bean.GroupMusicInfo;
import com.lzb.musicplayer.bean.MusicFile;
import com.lzb.musicplayer.utils.MusicScannerUtil;

public class InfoMusicAdapter extends ProviderMultiAdapter<GroupMusicInfo> {

    public InfoMusicAdapter() {
        addItemProvider(new InfoMusicIconProvider());
    }

    private class InfoMusicIconProvider extends BaseItemProvider<GroupMusicInfo> {
        @Override
        public int getItemViewType() {
            return ProviderMultiAdapter.TYPE_VIEW;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_info_music;
        }

        @Override
        public void convert(@NonNull BaseViewHolder baseViewHolder, GroupMusicInfo bean) {
            if (bean.getList() != null && bean.getList().size() > 0) {
                MusicFile musicFile = bean.getList().get(0);
                Bitmap albumCover = MusicScannerUtil.getAlbumArt(getContext(), musicFile.getAlbumCover());
                Bitmap bit = MusicScannerUtil.getAlbumArt(getContext(), musicFile.getArtistCover());
                baseViewHolder.setImageResource(R.id.ivInfoMusicItemImg, R.drawable.gequ);
                if (albumCover != null) {
                    baseViewHolder.setImageBitmap(R.id.ivInfoMusicItemImg, albumCover);
                } else {
                    if (bit != null) {
                        baseViewHolder.setImageBitmap(R.id.ivInfoMusicItemImg, bit);
                    }
                }
                if (!TextUtils.isEmpty(bean.getList().get(0).getTitle())) {
                    baseViewHolder.setText(R.id.tvInfoMusicItemName, bean.getList().get(0).getTitle());
                }
                if (!TextUtils.isEmpty(bean.getList().get(0).getArtist())) {
                    baseViewHolder.setText(R.id.tvInfoMusicItemArtist, bean.getList().get(0).getArtist());
                }
                baseViewHolder.setText(R.id.tvInfoMusicItemDuration, TimeUtil.getMmss(bean.getList().get(0).getDuration()));
            }
        }
    }
}